/**
 * 
 */
package hu.myai.gui.widgets.graph;

import hu.myai.gui.widgets.model.ILabelAdapter;
import hu.myai.gui.widgets.model.IScalar;
import hu.myai.gui.widgets.model.LabelRenderer;
import hu.myai.gui.widgets.model.Partition;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;

/**
 * @author Zsolt Safrany (u203697)
 * 
 */
public class GraphFactory {

    public static ScalarGraph createGraph(String type, Composite parent, Partition partition, IScalar scalar,
            final String title) {

        if (type.equalsIgnoreCase("basic")) {
            final ScalarGraph sg = new ScalarGraph(parent, SWT.DOUBLE_BUFFERED);
            sg.setMin(1.3);
            sg.setMax(1.4);
            sg.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_WHITE));
            sg.setForeground(parent.getDisplay().getSystemColor(SWT.COLOR_BLUE));
            sg.setPartition(partition);

            sg.setGridVisible(true);
            Grid2 grid = new Grid2(sg);
            grid.setGridLabelsVisible(true);
            grid.setGridLabelBackground(parent.getDisplay().getSystemColor(SWT.COLOR_BLACK));
            grid.setGridLabelForeground(parent.getDisplay().getSystemColor(SWT.COLOR_WHITE));
            grid.setGridColor(parent.getDisplay().getSystemColor(SWT.COLOR_BLACK));
            sg.setGrid(grid);

            ILabelAdapter adapter = new ILabelAdapter() {
                @Override
                public String getText() {
                    return title;
                }
            };

            LabelRenderer lr = new LabelRenderer();
            lr.setAdapter(adapter);
            lr.setX(0.7);
            lr.setY(0.0);
            lr.setBackgroundColor(parent.getDisplay().getSystemColor(SWT.COLOR_WHITE));
            lr.setForegroundColor(parent.getDisplay().getSystemColor(SWT.COLOR_BLACK));
            lr.setBorderColor(parent.getDisplay().getSystemColor(SWT.COLOR_BLACK));
            lr.setAlignment(LabelRenderer.ALIGN_CENTER);
            lr.setRelativeTo(LabelRenderer.TOP_LEFT);
            lr.setPaintBorder(true);
            sg.addPaintListener(lr);

            adapter = new ILabelAdapter() {
                @Override
                public String getText() {
                    return String.format("%+3.3f", sg.getScalar().getValue(0));
                }
            };

            lr = new LabelRenderer();
            lr.setAdapter(adapter);
            lr.setX(0.01);
            lr.setY(0.0);
            lr.setBackgroundColor(parent.getDisplay().getSystemColor(SWT.COLOR_WHITE));
            lr.setForegroundColor(parent.getDisplay().getSystemColor(SWT.COLOR_BLACK));
            lr.setBorderColor(parent.getDisplay().getSystemColor(SWT.COLOR_BLACK));
            lr.setAlignment(LabelRenderer.ALIGN_RIGHT);
            lr.setRelativeTo(LabelRenderer.BOTTOM_RIGHT);
            lr.setPaintBorder(true);
            sg.addPaintListener(lr);

            sg.setScalar(scalar);

            Menu menu = new Menu(parent.getShell(), SWT.POP_UP);
            MenuItem item = new MenuItem(menu, SWT.PUSH);
            item.setText("AutoZoom");
            item.addSelectionListener(new SelectionAdapter() {
                @Override
                public void widgetSelected(SelectionEvent e) {
                    sg.autoZoom();
                }
            });
            // sg.setMenu(menu);

            return sg;
        }
        throw new IllegalArgumentException("unknown type: \"" + type + "\"");
    }
}
